/**
* Casey Burkhardt
* CSC 2035 - Algorithms and Data Structures III
* Due: 09/22/2008
* ControlsPanel.java - Manages ControlsPanel Objects for the Airport Security Project
*/

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;


public class ControlsPanel extends JPanel
{
	//Constants
	int QUEUE_SIZE = 1000;
	
	// GUI Objects
	private JPanel buttonPanel;

	private JButton addButton;
	private JButton removeButton;
	private JButton startButton;
	private JButton stopButton;
	private JButton helpButton;
	
	private int count = 0;
	
	//Handlers
	private ButtonHandler buttonListener;
	private TimerHandler timerListener;
	
	private Timer mainTimer;
	private Random rand;
	
	//Programitic Timer Assignments
	private int nextEnter;
	private int nextExit;
	
	//Timer Delays
	private static int MAX_ENTER_TIME = 3000;
	private static int ENTER_SPEED = 1400;
	private static int EXIT_SPEED = 600;
	private static int TIMER_DELAY = 750;
	private static int MAX_EXIT_TIME = 3000;

	private int passengersOut = 0;
	private int time;
	private ImagePanel imagePanel;
	private AirportSecurityGUI GUI;
	private String mostRecentAction;
	private JTextField currentTime;
	private JTextField passengersInQueue;	
   
	//Array of Passengers
	Passenger[] passenger = new Passenger[QUEUE_SIZE];
	
	// Queue Management
	CircularArrayQueue mainQueue = new CircularArrayQueue(QUEUE_SIZE);

	
   public ControlsPanel(ImagePanel imagePanel,AirportSecurityGUI GUI)
   {
	   this.GUI = GUI;
	   this.currentTime = currentTime;
	   this.passengersInQueue = passengersInQueue;
	   this.imagePanel = imagePanel;
	   buttonListener = new ButtonHandler();
	   timerListener = new TimerHandler();
	   mainTimer = new Timer (TIMER_DELAY, timerListener);

	   // Add Button
	   addButton = new JButton("Add");
	   addButton.setToolTipText("Manually adds a passenger to the queue.");
	   addButton.addActionListener(buttonListener);
	   
	   // Remove Button
	   removeButton = new JButton("Remove");
	   removeButton.setToolTipText("Manually removes a passenger to the queue.");
	   removeButton.addActionListener(buttonListener);
		
		// Start Button
		startButton = new JButton("Start");
		startButton.setToolTipText("Starts the automated Airport Security program.");
		startButton.addActionListener(buttonListener);
		
		//Stop Button
		stopButton = new JButton("Stop");
		stopButton.setToolTipText("Starts the automated Airport Security program.");
		stopButton.addActionListener(buttonListener);
		
		//Stop Button
		helpButton = new JButton("Help");
		helpButton.setToolTipText("Provides help information.");
		helpButton.addActionListener(buttonListener);

		// Place buttonPanel on the GUI
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,2));
		buttonPanel.add(startButton);
		buttonPanel.add(stopButton);
		buttonPanel.add(addButton);
		buttonPanel.add(removeButton);
		buttonPanel.add(helpButton);
		buttonPanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		add(buttonPanel);
		
		//Initialize Random Seed
		rand = new Random();
	}

	class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (e.getActionCommand().equals("Add"))
			{
				addToQueue();
			}
			else if (e.getActionCommand().equals("Remove"))
			{
				removeFromQueue();
			}
			else if (e.getActionCommand().equals("Start"))
			{
				mainTimer.start();
			}	
			else if (e.getActionCommand().equals("Stop"))
			{
				mainTimer.stop();
			}
			else if (e.getActionCommand().equals("Help"))
			{
				JOptionPane.showMessageDialog(null, "Help Information:\n\nADD - Manually inserts a passenger into the queue.\nREMOVE - Manually removes a passenger from the queue\nSTART - Starts the Airport Security Simulation\nSTOP - Stops the Airport Security Simulation");  
			}
		}
	}
	class TimerHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			time++;
			time %= 1440;
			nextEnter = nextEnter - ENTER_SPEED;
			nextExit  = nextExit - EXIT_SPEED;
			if(nextEnter <= 0)
			{
				addToQueue();
			}
			if(nextExit <= 0)
			{
				removeFromQueue();
			}

			updateTime(timeString(time));
		}
	}
	
	private void addToQueue()
	{		
		imagePanel.addSprite();
		mainQueue.enQueue(new Passenger("Passenger Name", 12345678, time));
		GUI.updateCustomerCount();
		nextEnter = rand.nextInt(MAX_ENTER_TIME);
		count++;
	}
	
	private void removeFromQueue()
	{
		if (!imagePanel.isEmpty())
		{
			imagePanel.removeFrontSprite();	
			passengersOut++;
			GUI.updateCustomerCount();
			count--;
		}
		nextExit = rand.nextInt(MAX_EXIT_TIME);
	}
	
	private void updateTime(String s)
	{
		GUI.updateClock(s);
	}
	
	public String mostRecentAct()
    {
		return mostRecentAction;
	}
	
	public String timeString(int min)
	{
		boolean a;
		String result = new String("");
		
		int hour = min / 60;
		int minute = min % 60;
				
		if(hour >= 12)
			a = false;
		else
			a = true;
		
		if (hour >12) hour = hour - 12;
		if (hour == 0) hour = 12;
		if (hour <10) result = result + " ";
		result = result + new Integer((hour)).toString();
		result = result + ":";
		
		if (minute <10) result = result + "0";
		result = result + new Integer((minute)).toString() + " ";
		
		if(a)
			result = result + "AM";
		else
			result = result + "PM";
		return result;
	}
	
	public int getCurrentTime()
	{
	  return time;
	}
}